
package codeGenerate.mybatis.generater.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import codeGenerate.mybatis.generater.AbstractGenerater;
import codeGenerate.mybatis.mybaitsJdbcType.JdbcTypeHandle;
import codeGenerate.mybatis.mybaitsJdbcType.imp.BIGINT;
import codeGenerate.mybatis.mybaitsJdbcType.imp.INT;
import codeGenerate.mybatis.mybaitsJdbcType.imp.SMALLINT;
import codeGenerate.mybatis.mybaitsJdbcType.imp.TINYINT;
import codeGenerate.mybatis.mybaitsJdbcType.imp.VARCHAR;
import codeGenerate.mybatis.vo.ConfigVo;

public class ClickhouseGenerater extends AbstractGenerater {

	private final static Map<String, JdbcTypeHandle> map = new HashMap<>();
	static {
		map.put("STRING", new VARCHAR());

		map.put("INT8", new TINYINT());
		map.put("INT16", new SMALLINT());
		map.put("INT32", new INT());
		map.put("INT64", new BIGINT());

		map.put("UINT8", new INT());
		map.put("UINT16", new INT());
		map.put("UINT32", new INT());
		map.put("UINT64", new BIGINT());

		map.put("IPV4", new VARCHAR());
		map.put("IPV6", new VARCHAR());
	}

	@Override
	public Map<String, String> getColumnCommont() {
		Map<String, String> columnCommontMap = new HashMap<String, String>();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = getConnection().createStatement();
			ConfigVo configVo = super.getConfigVo();
			String sql = "SELECT name,type from   system.columns where table='" + configVo.getTblName().toUpperCase() + "'";
			if (StringUtils.isNotBlank(configVo.getDbName())) {
				sql = sql + " and database = '" + configVo.getDbName() + "'";
			}
			rs = st.executeQuery(sql);
			while (rs.next()) {
				columnCommontMap.put(rs.getString("name"), rs.getString("type"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return columnCommontMap;
	}

	@Override
	public JdbcTypeHandle registerTypeHandle(String columnTypeName) {
		return map.get(columnTypeName);
	}

	@Override
	public boolean isCustomColumnComment() {
		return false;
	}
}
